package br.uvv.sao.dao;

import java.util.List;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

import br.uvv.sao.domain.Perfil;
import br.uvv.sao.util.DAOException;

@SuppressWarnings("unchecked")
public class PerfilDAO extends DAO {
	
	private static final long serialVersionUID = -4540349069428672143L;
	
	public Perfil get(Long id) throws DAOException {
		return (Perfil) get(Perfil.class, id);
	}
	
	public Perfil findByNome(String nome) throws DAOException {
		return (Perfil) get(Perfil.class, null, Restrictions.eq("nome", nome));
	}

	public List<Perfil> listarPerfis(int first, int pageSize, String sortField, boolean sortOrder) throws DAOException {
		return listPaginator(first, pageSize, sortField, sortOrder, Perfil.class, null);
	}

	public void removerPerfil(Perfil perfilInstance) throws DAOException {
		delete(Perfil.class, perfilInstance.getId());
	}

	public void salvarPerfil(Perfil perfilInstance) throws DAOException {
		save(perfilInstance, perfilInstance.getId());
	}

	public Integer perfilCount() throws DAOException {
		return rowCount(Perfil.class);
	}

	public Integer validarNomeUnico(String nome, Long id) throws DAOException {
		Criterion[] filtros = new Criterion[2];
		if (nome!=null)
			filtros[0] = Restrictions.ilike("nome", nome);
		if (id!=null)
			filtros[0] = Restrictions.ne("id", id);
		return rowCount(Perfil.class, filtros);
	}
}
